//
//  XMConversationInfoView.m
//  XunMi
//
//  Created by MacBook on 2024/4/11.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMConversationInfoView.h"
#import "XMConversationInfoViewCell.h"
#import "XMRecentPhotosModel.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface XMConversationInfoView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *infoLa;
@property (weak, nonatomic) IBOutlet UIButton *cityLa;
@property (weak, nonatomic) IBOutlet UIImageView *realImgV;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionW;
@property (weak, nonatomic) IBOutlet UIButton *inviteButton;
@property (weak, nonatomic) IBOutlet UIButton *onLineButton;
@property (weak, nonatomic) IBOutlet UIButton *expandButton;
@property (weak, nonatomic) IBOutlet UILabel *qmLa;
@property (weak, nonatomic) IBOutlet UIView *bgView1;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *bgImgView;
@property (weak, nonatomic) IBOutlet UIImageView *qinmiImgV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hSpace;

@end

@implementation XMConversationInfoView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.infoLa setImagePosition:XMImagePositionLeft spacing:5];
    [self.cityLa setImagePosition:XMImagePositionLeft spacing:5];
    [self.onLineButton setImagePosition:XMImagePositionLeft spacing:3];
    [self.inviteButton setImagePosition:XMImagePositionTop spacing:4];
    kCornerRadius(_inviteButton, 6, 0, UIColor.clearColor);
    self.qinmiImgV.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qinmiAct)];
    [_qinmiImgV addGestureRecognizer:tap];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(40, 40);
    layout.minimumLineSpacing = 6;
    layout.minimumInteritemSpacing = 6;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMConversationInfoViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMConversationInfoViewCell)];
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1)];
    [self addGestureRecognizer:tap1];
}

- (void)setIntimacy:(NSString *)intimacy {
    _intimacy = intimacy;
    self.qmLa.text = kStringFormat(@"亲密度%@°", kValueToString(intimacy));
}

- (void)setModel:(XMUserModel *)model {
    _model = model;
    [self.infoLa setTitle:kStringFormat(@"基本信息：%@", model.describe) forState:UIControlStateNormal];
    [self.cityLa setTitle:kStringFormat(@"所在城市：%@", model.city) forState:UIControlStateNormal];
    
    self.qmLa.text = kStringFormat(@"亲密度%@°", kValueToString(model.intimacy));
    
    if (model.album_list.count > 0) {
        self.inviteButton.hidden = YES;
        self.collectionW.constant = model.album_list.count * 40 + (model.album_list.count - 1) * 6;
        self.hSpace.constant = 6;
        [self.collectionView reloadData];
    } else {
        self.inviteButton.hidden = NO;
        self.collectionW.constant = 0;
        self.hSpace.constant = 0;
    }
    
    if (model.is_online.intValue == 0) {
        [self.onLineButton setTitle:@"刚刚活跃" forState:UIControlStateNormal];
        [self.onLineButton setTitleColor:kHEX(@"#959595") forState:UIControlStateNormal];
        
    } else if (model.is_online.intValue == 1) {
        [self.onLineButton setTitle:@"在线" forState:UIControlStateNormal];
        [self.onLineButton setTitleColor:kHEX(@"#1F1F1F") forState:UIControlStateNormal];
    } else {
        [self.onLineButton setTitle:@"离线" forState:UIControlStateNormal];
        [self.onLineButton setTitleColor:kHEX(@"#959595") forState:UIControlStateNormal];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.album_list.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMConversationInfoViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMConversationInfoViewCell) forIndexPath:indexPath];
    XMRecentPhotosModel *model = self.model.album_list[indexPath.item];
    
    [cell.imgV sd_setImageWithURL:kStringToUrl(model.image.file_url) placeholderImage:kDefaultCoverImg];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XMConversationInfoViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    __block NSMutableArray *datas = [NSMutableArray array];
    [self.model.album_list enumerateObjectsUsingBlock:^(XMRecentPhotosModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 网络图片
        YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:obj.image.file_url];
        data.projectiveView = cell.imgV;
        [datas addObject:data];
    }];
    
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = indexPath.item;
    [browser show];
}

- (IBAction)expandAct:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.bgView1.hidden = sender.selected;
    if (sender.selected) {
        self.bgImgView.image = kGetImage(@"con_info_bg2");
    } else {
        self.bgImgView.image = kGetImage(@"con_info_bg1");
    }
    
    if (self.expandBlock) {
        self.expandBlock(sender.selected);
    }
}

- (void)tap1 {
    if (self.tapBlock) {
        self.tapBlock(self.model);
    }
}

- (void)qinmiAct {
    if (self.qinmiBlock) {
        self.qinmiBlock();
    }
}

@end
